package com.jy.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.pojo.Car;
import com.jy.service.CarService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("car")
public class CarController {
    @Autowired
    CarService carService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("findPage")
    public Page<Car> findPage(Integer pn, Integer ps, @RequestBody Car car) {
        return carService.findPage(pn, ps, car);
    }

    @RequestMapping("deleteIdArr")
    public boolean deleteIdArr(@RequestBody List<Integer> idArr) {
        carService.removeBatchByIds(idArr);
        for (Integer i : idArr) {
            rabbitTemplate.convertAndSend("car.delete", i);
        }
        return true;
    }

    @RequestMapping("deleteById")
    public boolean deleteById(Integer id) {
        carService.removeById(id);
        rabbitTemplate.convertAndSend("car.delete", id);
        return true;
    }

    @RequestMapping("upadd")
    public boolean upadd(@RequestBody Car car) {
        carService.saveOrUpdate(car);
        String carStr = JSON.toJSONString(car);
        rabbitTemplate.convertAndSend("car.upadd", carStr);
        return true;
    }

    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<Car> list = carService.findAll();
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();
        XSSFRow title = sheet.createRow(0);
        title.createCell(0).setCellValue("ID");
        title.createCell(1).setCellValue("车名");
        title.createCell(2).setCellValue("出厂时间");
        title.createCell(3).setCellValue("特征");
        title.createCell(4).setCellValue("是否上架");
        title.createCell(5).setCellValue("品牌");
        for (int i = 0; i < list.size(); i++) {
            Car car = list.get(i);
            XSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(car.getId());
            row.createCell(1).setCellValue(car.getName());
            row.createCell(2).setCellValue(car.getTime());
            row.createCell(3).setCellValue(car.getHobby());
            row.createCell(4).setCellValue(car.getStatus());
            row.createCell(5).setCellValue(car.getTname());
        }
        // 设置response的格式，文件格式
        response.setContentType("application/vnd.ms-excel");
        // 设置response的头
        response.setHeader("Content-Disposition", "attachment;filename=car.xlsx");
        // 6.把workbook写入到response
        workbook.write(response.getOutputStream());
    }

    @RequestMapping("uploadExcel")
    public boolean uploadExcel(MultipartFile excelFile) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(excelFile.getInputStream());
        XSSFSheet sheet = workbook.getSheetAt(0);
        int rows = sheet.getPhysicalNumberOfRows();
        for (int i = 1; i < rows; i++) {
            XSSFRow row = sheet.getRow(i);
            String name = row.getCell(1).getStringCellValue();
            Date time = row.getCell(2).getDateCellValue();
            String hobby = row.getCell(3).getStringCellValue();
            String statusStr = row.getCell(4).getStringCellValue();
            String tname = row.getCell(5).getStringCellValue();
            Integer tid = carService.findIdByTname(tname);
            Car car = new Car();

            car.setName(name);
            car.setTime(time);
            car.setHobby(hobby);
            car.setStatus(statusStr.equals("是") ? 0 : 1);
            car.setTid(tid);
            carService.save(car);
        }
        return true;
    }

    @RequestMapping("getCarReport")
    public Map getEmpReport() {
        List<Map> list=carService.findCount();
        List<String>xlist=new ArrayList<>();
        List<Long>ylist=new ArrayList<>();
        for (Map map : list) {
            String x = (String) map.get("x");
            Long y = (Long) map.get("y");
            xlist.add(x);
            ylist.add(y);
        }
        Map map = new HashMap();
        map.put("xlist",xlist);
        map.put("ylist",ylist);
        return map;
    }
}
